/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import Business.EmployeeBusiness;
import Business.PermissionBusiness;
import Business.TimeworkBusiness;
import Objects.Employee;
import Objects.Permission;
import Objects.Timework;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Nguyen Kim Manh
 */
public class PermissionDetail extends javax.swing.JDialog {

    /**
     * Creates new form PermissionManagement
     */
    public PermissionDetail(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
        initComponents();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(d.width / 2 - this.getSize().width / 2, (d.height - 40) / 2 - this.getSize().height / 2);
        try {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(EmployeeDetail.class.getName()).log(Level.SEVERE, null, ex);
        }

        tblModelEmployee = new DefaultTableModel();
        tblModelEmployee.addColumn("");
        tblModelEmployee.addColumn("Employee ID");
        tblModelEmployee.addColumn("Employee Name");
        tblModelEmployee.addColumn("Gender");
        tblModelEmployee.addColumn("Address");
        tblModelEmployee.addColumn("Phone");
        tblModelEmployee.addColumn("Regency");
        tblModelEmployee.addColumn("Timework");
        tblModelEmployee.addColumn("Account");

        jTable1.setModel(tblModelEmployee);
    }
    /**
     * this method used to receiver Permission object from FrmMain
     */
    private String perID;

    public void receiverPermission(Permission per) {
        txtPermissionID.setText(per.getPermissionID());
        txaDescription.setText(per.getPermissionDes());
        txtPermissionID.setEnabled(false);
        perID = per.getPermissionID();
        ArrayList<Employee> list = EmployeeBusiness.getEmployeeByPermission(per.getPermissionID());
        loadData(list);
    }
    private DefaultTableModel tblModelEmployee;

    /**
     * method load data from Employee table
     */
    private void loadData(ArrayList<Employee> list) {
        tblModelEmployee.setRowCount(0);
        for (int i = 0; i < list.size(); i++) {
            Vector a = new Vector();
            Employee em = list.get(i);
            a.addElement(i + 1);
            a.addElement(em.getEmployeeID());
            a.addElement(em.getEmployeeName());
            a.addElement(em.getEmployeeGender());
            a.addElement(em.getEmployeeAddress());
            a.addElement(em.getEmployeePhone());

            Permission per = PermissionBusiness.getPermissionById(em.getPermissionID());
            a.addElement(per.getPermissionDes());

            Timework tw = TimeworkBusiness.getTimeworkById(em.getTimeworkID());
            a.addElement(tw.getTimeworkDes());

            a.addElement(em.getEmployeeAccount());

            tblModelEmployee.addRow(a);
        }
    }

    /**
     * method update permission information
     */
    private void updatePermission() {
        if (txaDescription.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "-Type Permission Description please!", "Notification", JOptionPane.WARNING_MESSAGE);
        } else {
            int kq = PermissionBusiness.updatePermission(perID, txaDescription.getText().trim());
            if (kq > 0) {
                JOptionPane.showMessageDialog(null, "Update  Successful", "Notification", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Update Not Successful", "Notification", JOptionPane.WARNING_MESSAGE);
            }

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btnSave = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtPermissionID = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Permission Management");
        setPreferredSize(new java.awt.Dimension(860, 510));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 204, 204));
        jPanel1.setPreferredSize(new java.awt.Dimension(850, 500));

        jToolBar1.setBackground(new java.awt.Color(255, 204, 204));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnSave.setBackground(new java.awt.Color(255, 204, 204));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconSave.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        btnDelete.setBackground(new java.awt.Color(255, 204, 204));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconDelete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDelete);

        btnClose.setBackground(new java.awt.Color(255, 204, 204));
        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Presentation/image/IconClose.png"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClose);

        jLabel1.setText("Permission ID:");

        txtPermissionID.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        txaDescription.setColumns(20);
        txaDescription.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txaDescription.setRows(5);
        jScrollPane1.setViewportView(txaDescription);

        jLabel4.setText("Description:");

        jScrollPane2.setBackground(new java.awt.Color(255, 204, 204));
        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Employee List", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(52, 52, 52)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(txtPermissionID, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(66, 66, 66)
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(38, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(64, 64, 64)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPermissionID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(311, Short.MAX_VALUE)))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * method used to appear a popup dialog contain Employee information
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int index = jTable1.getSelectedRow();
        String emID = jTable1.getValueAt(index, 1).toString();
        ArrayList<Employee> listem = EmployeeBusiness.getEmployeeByID(emID);
        Employee em = listem.get(0);
        EmployeeDetail frm = new EmployeeDetail(null, "Employee Detail", true);
        frm.receiverEmployee(em);
        frm.setVisible(true);

        //load data from database when popup dialog is closed,information of Employee that updated or deleted will appear in jTable 
        ArrayList<Employee> list = EmployeeBusiness.getEmployeeByPermission(perID);
        loadData(list);
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * method delete Permission object
     */
    private void deletePermission() {
        int rs = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this permission?", "Notification", JOptionPane.YES_NO_OPTION);
        if (rs == 0) {
            String perID = txtPermissionID.getText();
            ArrayList<Employee> list = EmployeeBusiness.getEmployeeByPermission(perID);
            if (list.size() > 0) {
                JOptionPane.showMessageDialog(null, "You don't delete this Permission because it constraint with Employee Information", "Notification", JOptionPane.WARNING_MESSAGE);
            } else {
                String sql = "{call sp_deletePermission(?)}";
                ArrayList list1 = new ArrayList();
                list1.add(perID);
                try {
                    int kt = DBAccess.DB_Connect.executeNonQuery(sql, list1);
                    if (kt > 0) {
                        JOptionPane.showMessageDialog(null, "Delete Successful", "Notification", JOptionPane.INFORMATION_MESSAGE);
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "Delete NOT Successful", "Notification", JOptionPane.WARNING_MESSAGE);

                    }
                } catch (SQLException ex) {
                    Logger.getLogger(PermissionDetail.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(PermissionDetail.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        }
    }

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        updatePermission();
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        deletePermission();
    }//GEN-LAST:event_btnDeleteActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JTextField txtPermissionID;
    // End of variables declaration//GEN-END:variables
}
